package com.iflytek.cloud.thirdparty; import android.app.ActivityManager; import android.content.Context; import android.content.SharedPreferences; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import com.facebook.ads.AudienceNetworkActivity; import com.iflytek.cloud.SpeechConstant; import com.iflytek.cloud.SpeechError; import com.iflytek.cloud.Version; import com.iflytek.cloud.thirdparty.L; import com.mopub.mobileads.VastExtensionXmlManager; import java.text.DecimalFormat; import java.util.List; import java.util.Locale; import java.util.Map; import org.apache.http.util.EncodingUtils; import org.json.JSONArray; import org.json.JSONObject; public class Q { private static Q a = null; private static Context b = null; private static SharedPreferences c; private boolean d = false; private boolean e = false; private boolean f = false; private boolean g = false; private boolean h = false; private long i = 0; private long j = 0; private long k = 0; private long l = 0; private long m = 0; private long n = 43200; private L.a o = new L.a() { /* class com.iflytek.cloud.thirdparty.Q.AnonymousClass3 */ @Override // com.iflytek.cloud.thirdparty.L.a public void a(SpeechError speechError) { Q.this.d = false; O.d("CollectInfo", new StringBuilder().append(speechError.getErrorCode()).toString()); } @Override // com.iflytek.cloud.thirdparty.L.a public void a(L l, byte[] bArr) { Q q; if (bArr != null) { try { JSONObject jSONObject = new JSONObject(EncodingUtils.getString(H.c(bArr), AudienceNetworkActivity.WEBVIEW_ENCODING)); O.d("CollectInfo", "策略请求结果: " + jSONObject.toString()); if ("yes".equalsIgnoreCase(jSONObject.optString("is_collect"))) { Q.this.h = true; } else { Q.this.h = false; } Q.this.i = (long) (Double.parseDouble(jSONObject.optString("ti_request")) * 3600.0d); Q.this.j = (long) (Double.parseDouble(jSONObject.optString("ti_app_list")) * 3600.0d); Q.this.k = (long) (Double.parseDouble(jSONObject.optString("ti_app_active")) * 3600.0d); SharedPreferences.Editor edit = Q.c.edit(); edit.putBoolean("is_collect", Q.this.h); edit.putLong("ti_request", Q.this.i); edit.putLong("ti_app_list", Q.this.j); edit.putLong("ti_app_active", Q.this.k); edit.commit(); } catch (Throwable th) { Q.this.d = false; throw th; } } q = Q.this; q.d = false; } }; private L.a p = new L.a() { /* class com.iflytek.cloud.thirdparty.Q.AnonymousClass4 */ @Override // com.iflytek.cloud.thirdparty.L.a public void a(SpeechError speechError) { Q.this.e = false; O.d("CollectInfo", new StringBuilder().append(speechError.getErrorCode()).toString()); } @Override // com.iflytek.cloud.thirdparty.L.a public void a(L l, byte[] bArr) { Q q; if (bArr != null) { try { O.d("CollectInfo", "上传数据结果返回: " + EncodingUtils.getString(H.c(bArr), AudienceNetworkActivity.WEBVIEW_ENCODING)); } catch (Throwable th) { Q.this.e = false; throw th; } } q = Q.this; q.e = false; } }; private Q(Context context) { if (context != null) { b = context.getApplicationContext(); c = b.getSharedPreferences("iflytek_state_" + b.getPackageName(), 0); this.h = c.getBoolean("is_collect", false); this.i = c.getLong("ti_request", 0); this.j = c.getLong("ti_app_list", this.n); this.l = c.getLong("list_app_time", 0); this.k = c.getLong("ti_app_active", this.n); this.m = c.getLong("active_app_time", 0); } } public static Q a(Context context) { if (a == null) { a = new Q(context); } return a; } private static JSONObject a(JSONObject jSONObject, JSONObject jSONObject2) { JSONObject jSONObject3 = new JSONObject(); try { jSONObject3.put("header", jSONObject2); jSONObject3.put("body", jSONObject); } catch (Throwable th) { O.b(th); } return jSONObject3; } private static JSONObject a(boolean z, R r, String str) { JSONObject jSONObject = new JSONObject(); JSONObject jSONObject2 = new JSONObject(); for (Map.Entry<String, String> entry : r.c().entrySet()) { try { jSONObject2.put(entry.getKey(), entry.getValue()); } catch (Throwable th) { O.b(th); } } jSONObject.put(str, jSONObject2); return z ? jSONObject : jSONObject2; } private void a(JSONObject jSONObject) { try { if (M.b(b)) { byte[] bytes = jSONObject.toString().getBytes(AudienceNetworkActivity.WEBVIEW_ENCODING); byte[] b2 = H.b(bytes); L l2 = new L(); l2.b(20000); l2.a(1); l2.a("http://scs.openspeech.cn/scs", "cmd=statsdklog&logver=1.0.2&size=" + bytes.length, b2); l2.a(this.p); return; } this.e = false; } catch (Throwable th) { this.e = false; O.b(th); } } private static JSONObject b(Context context) { R b2 = D.b(context).clone(); T.a(context, b2); b2.a("appid", T.a()); b2.a("unique_id", N.a(context)); b2.a("src", SpeechConstant.MODE_MSC); b2.a("ver", Version.getVersion()); b2.a("lang", Locale.getDefault().getLanguage()); b2.a("logtime", new StringBuilder().append(System.currentTimeMillis()).toString()); JSONObject a2 = a(false, b2, "header"); try { DecimalFormat decimalFormat = new DecimalFormat("#.########"); a2.put("lat", decimalFormat.format((double) E.a(context).a("msc.lat"))); a2.put("lng", decimalFormat.format((double) E.a(context).a("msc.lng"))); } catch (Throwable th) { O.b(th); } return a2; } private boolean d() { try { return (System.currentTimeMillis() / 1000) - c.getLong("request_time", 0) > c.getLong("ti_request", 0); } catch (Throwable th) { O.b(th); return true; } } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private void e() { try { JSONObject jSONObject = new JSONObject(); jSONObject.put("pver", "3"); jSONObject.put(VastExtensionXmlManager.TYPE, "app_list"); jSONObject.put("appid", T.a()); jSONObject.put("src", SpeechConstant.MODE_MSC); O.d("CollectInfo", jSONObject.toString()); if (M.b(b)) { byte[] b2 = H.b(jSONObject.toString().getBytes(AudienceNetworkActivity.WEBVIEW_ENCODING)); L l2 = new L(); l2.b(20000); l2.a(1); l2.a("http://data.openspeech.cn/index.php/clientrequest/clientcollect/isCollect", "", b2); l2.a(this.o); SharedPreferences.Editor edit = c.edit(); edit.putLong("request_time", System.currentTimeMillis() / 1000); edit.commit(); return; } this.d = false; } catch (Throwable th) { this.d = false; O.b(th); } } private boolean f() { if (!this.h) { return false; } long currentTimeMillis = System.currentTimeMillis() / 1000; this.f = currentTimeMillis - this.l > this.j; this.g = currentTimeMillis - this.m > this.k; return this.f || this.g; } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private void g() { JSONArray i2; JSONArray h2; SharedPreferences.Editor edit = c.edit(); if (this.f) { this.l = System.currentTimeMillis() / 1000; O.d("CollectInfo", "lastListAppTime:" + this.l); edit.putLong("list_app_time", this.l); } if (this.g) { this.m = System.currentTimeMillis() / 1000; O.d("CollectInfo", "lastActiveAppTime:" + this.m); edit.putLong("active_app_time", this.m); } edit.commit(); try { JSONArray jSONArray = new JSONArray(); if (this.f && (h2 = h()) != null) { JSONObject jSONObject = new JSONObject(); jSONObject.put("appinfo", h2); jSONObject.put("ts", System.currentTimeMillis()); jSONArray.put(jSONObject); } if (this.g && (i2 = i()) != null) { JSONObject jSONObject2 = new JSONObject(); jSONObject2.put("hisinfo", i2); jSONObject2.put("ts", System.currentTimeMillis()); jSONArray.put(jSONObject2); } JSONObject jSONObject3 = new JSONObject(); jSONObject3.put("log", jSONArray); JSONObject a2 = a(jSONObject3, b(b)); O.d("CollectInfo", a2.toString()); a(a2); } catch (Throwable th) { this.e = false; O.b(th); } } private JSONArray h() { try { JSONArray jSONArray = new JSONArray(); PackageManager packageManager = b.getPackageManager(); List<PackageInfo> installedPackages = packageManager.getInstalledPackages(0); int size = installedPackages.size(); for (int i2 = 0; i2 < size; i2++) { PackageInfo packageInfo = installedPackages.get(i2); if ((packageInfo.applicationInfo.flags & 1) == 0) { JSONObject jSONObject = new JSONObject(); jSONObject.put(packageInfo.packageName, packageInfo.applicationInfo.loadLabel(packageManager).toString()); jSONArray.put(jSONObject); } } return jSONArray; } catch (Throwable th) { O.b(th); return null; } } private JSONArray i() { try { JSONArray jSONArray = new JSONArray(); PackageManager packageManager = b.getPackageManager(); for (ActivityManager.RecentTaskInfo recentTaskInfo : ((ActivityManager) b.getSystemService("activity")).getRecentTasks(20, 1)) { ResolveInfo resolveActivity = packageManager.resolveActivity(recentTaskInfo.baseIntent, 0); if (resolveActivity != null) { JSONObject jSONObject = new JSONObject(); jSONObject.put(resolveActivity.activityInfo.packageName, resolveActivity.loadLabel(packageManager).toString()); jSONArray.put(jSONObject); } } return jSONArray; } catch (Throwable th) { O.b(th); return null; } } public synchronized void a() { if (!this.d) { this.d = true; if (d()) { new Thread(new Runnable() { /* class com.iflytek.cloud.thirdparty.Q.AnonymousClass1 */ public void run() { Q.this.e(); } }).start(); } else { this.d = false; } } } public synchronized void b() { if (!this.e) { this.e = true; if (f()) { new Thread(new Runnable() { /* class com.iflytek.cloud.thirdparty.Q.AnonymousClass2 */ public void run() { Q.this.g(); } }).start(); } else { this.e = false; } } } }